<?php

//use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AnadirDatosPordefectoTablasCGTN extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        $this->addDefecto('genero', Genero::getGeneros());
        $this->addDefecto('color', Color::getColores());
        $this->addDefecto('tamano', Tamano::getTamanos());
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        //Eliminar todos los generos
        foreach (Genero::all() as $obj) {
            $obj->delete();
        }
        //Eliminar todos los colores
        foreach (Color::all() as $obj) {
            $obj->delete();
        }
        //Eliminar todos los tamaños
        foreach (Tamano::all() as $obj) {
            $obj->delete();
        }
    }

    /**
     * Añade los valores por defecto a la BBDD de forma persistente
     * @param type $que
     * @param type $list
     */
    private function addDefecto($que, $list = array()) {
        foreach ($list as $id => $nombre) {
            $class = ucfirst($que);
            $obj = new $class();
            $obj->id = $id;
            $obj->tipo = $nombre;
            $obj->save();
        }
    }

}
